package org.cicd.devops

import org.cicd.enums.*
import org.cicd.modle.AppInfoVars
import org.cicd.utils.utils


/**
 * 检出项目代码
 * @param srcUrl
 * @param branchName
 * @return
 */
def checkOutCode(AppInfoVars appInfoVars, appMap) {
    def utils = new utils()
    utils.printMessage("获取代码", "green")
    checkOut(appInfoVars.repositoryUrl, appInfoVars.branchName)
    //初始化包信息
    return getGav2(appInfoVars)
}
/**
 * 检出代码
 * @param srcUrl
 * @param branchName
 * @return
 */
def checkOut(srcUrl, branchName) {
    // Jenkins git插件
    checkout([$class                           : 'GitSCM', branches: [[name: "${branchName}"]],
              doGenerateSubmoduleConfigurations: false,
              extensions                       : [],
              submoduleCfg                     : [],
              userRemoteConfigs                : [[credentialsId: 'bitbucket_secret', url: "${srcUrl}"]]])
}


def gitClone(srcUrl, branchName) {
    def utils = new utils();
    utils.exeShell("git clone -b ${branchName} ${srcUrl}")
}

def getGav2(AppInfoVars appInfo) {
    def utils = new utils()
    def pomPath = "${workspace}"+utils.getFileSeparator()+"pom.xml";
    def packagePath = "${workspace}"+utils.getFileSeparator()+"package.json"
    def deployPropertiesPath =  "${workspace}"+utils.getFileSeparator()+"deploy.properties"

    def buildUser = getBuildUser()
    appInfo.BUILD_USER = buildUser
    appInfo.repositoryUrl = utils.getGitRepUrl()

    boolean isMvn = utils.fileExist(pomPath)
    boolean isNpm = utils.fileExist(packagePath)
    if(isMvn) {
        def pom = utils.loadMavenPom(pomPath)
        appInfo.appId = "${pom.artifactId}"
        appInfo.appName = "${pom.artifactId}"
        appInfo.appVersion = "${pom.version}"
        appInfo.buildType = BuildType.MVN
    } else if (isNpm){
        def prop = utils.loadJson(packagePath)
        appInfo.appId = "${prop.name}"
        appInfo.appName = "${prop.name}"
        appInfo.appVersion = "${prop.version}"
        appInfo.buildType = BuildType.NPM
    }

    boolean needPackage = utils.fileExist(deployPropertiesPath)
    appInfo.needPackage = needPackage
    if(needPackage) {
        def deploy_properties = utils.loadProperties(deployPropertiesPath)
        if(null==deploy_properties) {
            utils.printErrorMessage("项目根目录 下 deploy.properties 文件 配置缺失，请填写正确的信息，格式如: {分支名称}={dev|test|staging}, 例子:feature/1.0.0=dev, release/1.0=test","red")
            return
        }
        def deployBranch = "dev"
        def deployEnvNames = "dev"
        deploy_properties.each {
            deployBranch = it.key
            deployEnvNames = it.value
        }
        appInfo.branchName = deployBranch
        appInfo.deployEnvNames = deployEnvNames.split(",")
    }
    return appInfo;
}


def checkOutPackageGitRep(EnvInfoType envInfoType) {
    def utils = new utils()
    if(appInfoVars.buildType ==BuildType.MVN) {
        def gitPath = "${workspace}"+utils.getFileSeparator()+"target"
        dir(gitPath) {
            def packageRepUrl = utils.getZipPackageGitReposity(envInfoType);
            checkOut(packageRepUrl, envInfoType.name())

        }
    }
}